/*
Project : Programmable Control of Airflow System for Maintaining Oxygen Saturation in Pre-term infant 
Microcontroller : STM32F4 Discovery (STM32F407VG)
File : main.c
Deverloper : Phattaradanai Kiratiwudhikul
Reseach & Deverloped by Department of Electrical Engineering, Faculty of Engineering, Mahidol University
*/
//------------------------------------------------------------------------------
#include "main.h"
#include <stdlib.h>
#include "driver_dac_LTC1661.h"
#include "driver_MCP3202.h"
#include "driver_control_valve.h"
#include "driver_system_init.h"
#include "driver_oxygen_pulse_meter.h"
#include "driver_oxygen_sensor.h"
#include "driver_glcd5110.h"
#include "define_pin.h"
#include "driver_GUI_connect.h"
#include "testControlValve.h"
#include "driver_check_status_profile.h"
#include "driver_alarm_condition.h"

#include "task_GUI_connect.h"
#include "task_oxygen_pulse_meter.h"

// extern Profile Structger-----------------------------------------------------
extern Profile SProfile;

//------------------------------------------------------------------------------
void Alarm_Function(uint8_t Command);
void Timer3_Config(void);

//void vApplicationStackOverflowHook(void);
//void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );

// Variable --------------------------------------------------------------------
unsigned char msg ;
char Character;
uint32_t count;
uint8_t uiSD_Test[50];
char cSD_String[50];
uint8_t index = 0;                                                              //for count receving Data form Hyperterminal for controling Drive Circuit
char cDataFromOPM_TEST[3];

float FiO2_Current;
float FiO2_Buffer[10];
char cFiO2_Buffer_String[50];
char FiO2_Test_String[120];
uint8_t uiFiO2_index;
uint8_t uiPurpose_FiO2, uiDrive_FiO2;
float FiO2_percentage;

uint8_t Sampling_time;

extern uint8_t uiOxygenSat_buffer[10];
extern uint8_t uiSD_Card_index;
extern uint8_t uiRx_index_OPM;
extern uint8_t uiCurrent_SpO2;
extern float FiO2_DataTest[24];

uint8_t uiCurrent_Status;
uint8_t Time_AlarmLevel = 0;

// Profile Variable ------------------------------------------------------------
char HospitalNumber_File[13];
char Buffer[128];
  
// Main Function ---------------------------------------------------------------
int main()
{ 
  /* Set Up config System*/
  start_GUI_task();
  
  
  vTaskStartScheduler();
}
	

//------------------------------------------------------------------------------
void vApplicationTickHook(void)
{
 
}
//------------------------------------------------------------------------------
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
	( void ) pcTaskName;
	( void ) pxTask;

	/* Run time stack overflow checking is performed if
	configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
	function is called if a stack overflow is detected. */
	taskDISABLE_INTERRUPTS();
	for( ;; );
}
//------------------------------------------------------------------------------
void vApplicationMallocFailedHook(void)
{
  //taskDISABLE_INTERRUPTS();
  while(1);
}
//------------------------------------------------------------------------------
#ifdef USE_FULL_ASSERT
/**
* @brief  assert_failed
*         Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  File: pointer to the source file name
* @param  Line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {}
}
#endif
// End of File -----------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
(C) Copyright 2014, Department of Electrical Engineering, Faculty of Engineering, Mahidol University
--------------------------------------------------------------------------------------------------*/